<template>
  <div ref="editor" class="text-editor"></div>
</template>

<script lang="ts">
import Vue from "vue";
import E from "wangeditor";
import { uploadCourseImage } from "@/services/course";

export default Vue.extend({
  name: "TextEditor",
  props: {
    value: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      editor: null
    };
  },
  watch: {
    value() {
      (this.editor as any).txt.html(this.value);
    }
  },
  // 组件已经渲染好，可以初始化操作 DOM 了
  mounted() {
    this.initEditor();
  },
  methods: {
    initEditor() {
      const editor = new E(this.$refs.editor as any);
      this.editor = editor as any;
      // 注意：事件监听必须在 create 之前
      editor.config.onchange = (value: string) => {
        this.$emit("input", value);
      };
      editor.create();

      // 注意：设置初始化必须在 create 之后
      editor.config.customUploadImg = function(
        resultFiles: any,
        insertImgFn: any
      ) {
        // resultFiles 是 input 中选中的文件列表
        // insertImgFn 是获取图片 url 后，插入到编辑器的方法
        // 上传图片，返回结果，将图片插入到编辑器中
        resultFiles.forEach(async (file: File) => {
          try {
            const fd = new FormData();
            fd.append("file", file);
            const { name } = await uploadCourseImage(fd);
            insertImgFn(name);
          } catch (e) {
            console.log(e);
          }
        });
      };
    }
  }
});
</script>
